# LuckPay Web Manage System
#
# Copyright (c) 2016 Lucky Byte, Inc.
#
$ ->
    # 表单字段验证
    form = $('form.main')
    form.form
        inline: true,
        fields:
            merch_type:  optional: false, rules: [ type: 'empty' ]
            wizard_uuid: optional: false, rules: [ type: 'empty' ]

    # 改变商户类型时，更新商户列表
    form.find('.dropdown.merch_type').dropdown
        onChange: (value, text, $choice) ->
            $(this).addClass 'loading'
            $.post '/wizcheck/ajax/wizard_uuids', {
                merch_type: value
            }, (resp) ->
                form.find('.dropdown.merch_type').removeClass 'loading'
                form.find('.dropdown.wizard_uuid').dropdown 'clear'
                if not resp.succ
                    form.find('.dropdown.wizard_uuid .menu').html ''
                    return
                items = ''
                for record in resp.records
                    items += "<div class='item' data-value='#{record.uuid}'>"
                    items += "#{record.merno}"
                    items += "<div class='right floated'
                        style='font-size:small; color:#888;'>#{record.mername}"
                    items += " ｜ #{record.termid}"
                    items += "</div>"
                    items += "</div>"
                form.find('.dropdown.wizard_uuid .menu').html items
                form.find('.dropdown.wizard_uuid').dropdown 'refresh'

    form.submit (evt) ->
        return if not form.form 'is valid'

        merch_type = form.form 'get value', 'merch_type'
        form.attr 'action', "/wizcheck/#{merch_type}"
